<?php

class Links_Action_default extends Kernel_Action_Abstract
{
    
    /**
     * Path to the file template.
     *
     * @var string
     */
    protected $_template = 'links/default.tpl';
    
    /**
     * Реализация обработки пользовательского запроса.
     *
     */
    protected function _process()
    {
        Application::getResponse()->assign('links', $this->_getLinks());
    }
    
    private function _getLinks() 
    {
        $query = '
            SELECT 
                   -- `c_l`.`id` `category_id`, 
                   `c_l`.`name` `category_name`, 
                   `l`.`name` `name`, 
                   `l`.`url` `url`
              FROM 
                   `links_categories` `c_l`
        INNER JOIN 
                   `links` `l` ON `l`.`category_id` = `c_l`.`id`
          ORDER BY 
                   `c_l`.`position` ASC, `l`.`name` ASC
        ';
        
        $links = array();
        foreach (database::fetchAll($query) as $entry) {
            $links[$entry['category_name']][] = array_slice($entry, 1);
        }
        
        return $links;
    }
    
}
